import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import tseslint from '@typescript-eslint/eslint-plugin'
import tsParser from '@typescript-eslint/parser'

export default [
  {
    ignores: [
      'dist',
      'src-tauri',
      'tests/**/*',
      'scripts/**/*',
      '*.config.js',
      '*.config.ts',
      'playwright.config.ts',
      'vite.config.ts',
      'vitest.config.ts'
    ],
  },
  // Configuration files and Node.js scripts
  {
    files: ['*.config.{js,ts}', 'scripts/**/*.{js,ts}', 'tests/**/*.{js,ts}'],
    languageOptions: {
      ecmaVersion: 2020,
      globals: { ...globals.browser, ...globals.node },
      parser: tsParser,
      parserOptions: {
        ecmaVersion: 'latest',
        sourceType: 'module',
      },
    },
    plugins: {
      '@typescript-eslint': tseslint,
    },
    rules: {
      ...js.configs.recommended.rules,
      '@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
      '@typescript-eslint/no-explicit-any': 'off',
      'no-console': 'off',
      'prefer-const': 'error',
      'no-var': 'error',
    },
  },
  // React/Browser code
  {
    files: ['src/**/*.{ts,tsx}'],
    languageOptions: {
      ecmaVersion: 2020,
      globals: globals.browser,
      parser: tsParser,
      parserOptions: {
        ecmaVersion: 'latest',
        sourceType: 'module',
        ecmaFeatures: {
          jsx: true,
        },
      },
    },
    plugins: {
      '@typescript-eslint': tseslint,
      'react-hooks': reactHooks,
      'react-refresh': reactRefresh,
    },
    rules: {
      // 基础规则 - 只保留最重要的
      'no-unused-vars': 'off', // 关闭基础规则，使用 TypeScript 版本
      '@typescript-eslint/no-unused-vars': ['warn', {
        argsIgnorePattern: '^_',
        varsIgnorePattern: '^_',
        ignoreRestSiblings: true
      }],
      '@typescript-eslint/no-explicit-any': 'off', // 允许使用 any
      'no-console': 'off', // 允许 console
      'prefer-const': 'warn', // 降级为警告
      'no-var': 'error',
      'no-undef': 'off', // TypeScript 会处理这个
      'no-redeclare': 'off', // TypeScript 会处理这个
      'no-prototype-builtins': 'off', // 允许使用 hasOwnProperty

      // React 相关规则
      'react-hooks/exhaustive-deps': 'warn', // 降级为警告
      'react-refresh/only-export-components': 'off', // 关闭这个限制

      // TypeScript 特定规则
      '@typescript-eslint/no-redeclare': 'off', // 允许重新声明
      '@typescript-eslint/ban-ts-comment': 'off', // 允许 @ts-ignore 等
    },
  },
]
